package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SDetailMapper;
import com.yuelife.service.pojo.SDetail;
import com.yuelife.service.service.SDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SDetail业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SDetailServiceImpl implements SDetailService {

    @Autowired
    private SDetailMapper sDetailMapper;


    /**
     * SDetail条件+分页查询
     * @param sDetail 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SDetail> findPage(SDetail sDetail, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sDetail);
        //执行搜索
        return new PageInfo<SDetail>(sDetailMapper.selectByExample(example));
    }

    /**
     * SDetail分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SDetail> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SDetail>(sDetailMapper.selectAll());
    }

    /**
     * SDetail条件查询
     * @param sDetail
     * @return
     */
    @Override
    public List<SDetail> findList(SDetail sDetail){
        //构建查询条件
        Example example = createExample(sDetail);
        //根据构建的条件查询数据
        return sDetailMapper.selectByExample(example);
    }


    /**
     * SDetail构建查询对象
     * @param sDetail
     * @return
     */
    public Example createExample(SDetail sDetail){
        Example example=new Example(SDetail.class);
        Example.Criteria criteria = example.createCriteria();
        if(sDetail!=null){
            // 详情主键
            if(!StringUtils.isEmpty(sDetail.getDetailId())){
                    criteria.andEqualTo("detailId",sDetail.getDetailId());
            }
            // 店铺id
            if(!StringUtils.isEmpty(sDetail.getStoreId())){
                    criteria.andEqualTo("storeId",sDetail.getStoreId());
            }
            // 画册图片
            if(!StringUtils.isEmpty(sDetail.getAlbumPics())){
                    criteria.andEqualTo("albumPics",sDetail.getAlbumPics());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sDetailMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SDetail
     * @param sDetail
     */
    @Override
    public void update(SDetail sDetail){
        sDetailMapper.updateByPrimaryKey(sDetail);
    }

    /**
     * 增加SDetail
     * @param sDetail
     */
    @Override
    public void add(SDetail sDetail){
        sDetailMapper.insert(sDetail);
    }

    /**
     * 根据ID查询SDetail
     * @param id
     * @return
     */
    @Override
    public SDetail findById(Integer id){
        return  sDetailMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SDetail全部数据
     * @return
     */
    @Override
    public List<SDetail> findAll() {
        return sDetailMapper.selectAll();
    }
}
